﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.Common;
using System.Text.RegularExpressions;
using HotelManagement.BL;

namespace HotelManagement.PL
{
    public partial class UserEditForm : Form
	{
		#region Varible
		private User m_UserInfo;
        private bool m_bIsUpdate;
        private UserBL userMng = new UserBL();
		public User UserInfo
		{
			get { return m_UserInfo; }
			set { m_UserInfo = value; }
		}
		#endregion

		#region Constructor
		public UserEditForm(bool isEdit)
        {
            InitializeComponent();
            m_bIsUpdate = isEdit;

		}
		#endregion

		#region Event Handler
		private void UserEditForm_Load(object sender, EventArgs e)
        {
            if (m_bIsUpdate)
            {
                this.Text = "编辑用户";
            }
            else
            {
                this.Text = "添加用户";
            }


            if (null != m_UserInfo && m_bIsUpdate)
            {
                txtName.Text = m_UserInfo.Name;
                txtPassword.Text = m_UserInfo.Password;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }
            if (m_bIsUpdate)
            {
                UpdateUser();
            }
            else
            {
                AddUser();
            }
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion

		#region Private Method
		private void UpdateUser()
        {
            if (0 <= userMng.UpdateUser(BuildUserInfo()))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				//UpdateParent();
				this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void AddUser()
        {
            if (0 <= userMng.AddUser(BuildUserInfo()))
            {
                MessageBox.Show("操作成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				//UpdateParent();
				this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("操作失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool CheckInput()
        {
            bool bReturn = true;
            string strEnNumPattern = @"[a-zA-Z\d]+$";

            if (!Regex.IsMatch(txtName.Text, strEnNumPattern))
            {
                MessageBox.Show("用户名称输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (!Regex.IsMatch(txtPassword.Text, strEnNumPattern))
            {
                MessageBox.Show("密码输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
           
            return bReturn;
        }

        private User BuildUserInfo()
        {
            User user = new User();
            if (m_UserInfo != null) 
            {
                user.ID = m_UserInfo.ID;
            }
            
            user.Name = txtName.Text;
            user.Password = txtPassword.Text;
            return user;
        }
		#endregion
	}
}
